#include "Game.h"

Game:: Game()
{
	// background
	element0 = new Sprite("element_0.png", 0, 0, DEFAULT_WIN_W * 0.5f, DEFAULT_WIN_H * 0.5f);

	SpawnCharacter(PLAYER_1, 100, 100);
	SpawnCharacter(PLAYER_2, 250, 150);

	// first plateform
	element1 = new Sprite("element_1.png", 50, 200);
	element1->CreateCollider(Collider::SQUARE_COLLIDER);

	// second plateform
	element2 = new Sprite("element_2.png", 0, 285);
	element2->CreateCollider(Collider::SQUARE_COLLIDER);

	// score display
	live0 = new Text("arial.ttf", 18, 10, 10);
	live1 = new Text("arial.ttf", 18, 650, 10);

	// play song
	Audio::GetInstance()->PlayMusic("main.mp3", 50, 500);

	// play intro
	Audio::GetInstance()->PlaySoundFocus("begin.wav");
}

Game::~Game()
{
	for (unsigned int i = 0; i < players.size(); i++) {
		delete players[i];
	}
	
	players.clear();
}

void Game::SpawnCharacter(Players num, int x, int y)
{
	Character* player = NULL;
	switch (num)
	{
		case PLAYER_1: {
			player = new Character("player_1.png", x, y);
		} break;

		case PLAYER_2: {
			player = new Character("player_2.png", x, y);
		} break;

		default:
		{
		} break;
	}

	if (player) {
		player->SetCurrentDirection(SpriteAnimation::LEFT);

		SDL_Rect frame = { 0, 0, 24, 24 };
		player->AddAnimationFrame(SpriteAnimation::RIGHT, frame);
		frame.x += 24;

		player->AddAnimationFrame(SpriteAnimation::RIGHT, frame);
		frame.x -= 24;
		frame.y += 24;

		player->AddAnimationFrame(SpriteAnimation::LEFT, frame);
		frame.x += 24;

		player->AddAnimationFrame(SpriteAnimation::LEFT, frame);

		player->CreateCollider(Collider::PLAYER_COLLIDER, 1.f);
		player->GetBody()->SetGravityScale(5.f);
		player->SetRotationFixed();

		b2Filter fd1;
		fd1.categoryBits = CollisionCategories::PLAYER_UPPER_CATEGORY;
		player->GetBody()->GetFixtureList()[0].SetFilterData(fd1);

		b2Filter fd2;
		fd2.categoryBits = CollisionCategories::PLAYER_LOWER_CATEGORY;
		player->GetBody()->GetFixtureList()[1].SetFilterData(fd2);
		players.push_back(player);
	}
}

void Game::Notify(const InputEvent& event)
{
	if (event.is_keyboard_key_pressed())
	{
		switch (event.get_keyboard_key())
		{
			case InputEvent::KEY_UP: {
				if (players.size() >= 1)
				{
					players[0]->Jump();
				}
			} break;

			case InputEvent::KEY_w: {
				if (players.size() >= 2)
				{
					players[1]->Jump();
				}
			} break;
		}
	}
}

void Game::Update(unsigned int t)
{
	if (players.size() >= 1) {
		live0->SetText(StringConcat() << "Player 1: " << players[0]->GetNumLives(), 0, 0, 255, 255);
		if (InputEvent::is_key_down(InputEvent::KEY_RIGHT)) {
			players[0]->Move( 8.0f);
		}

		if (InputEvent::is_key_down(InputEvent::KEY_LEFT)) {
			players[0]->Move(-8.0f);
		}
	}

	if (players.size() >= 2) {
		live1->SetText(StringConcat() << "Player 2: " << players[1]->GetNumLives(), 0, 255, 0, 255);
		if (InputEvent::is_key_down(InputEvent::KEY_d)) {
			players[1]->Move( 8.0f);
		}

		if (InputEvent::is_key_down(InputEvent::KEY_a)) {
			players[1]->Move(-8.0f);
		}
	}

	for (unsigned int i = 0; i < players.size(); i++) {
		if (players[i]->GetNumLives() <= 0)
		{
			Audio::GetInstance()->PlaySoundFocus("begin.wav");
			for (unsigned int j = 0; j < players.size(); j++) {
				players[j]->Reset();
			} return;
		}

		if (players[i]->GetBody()->GetPosition().y > DEFAULT_WIN_H * 0.5f ||
			players[i]->GetBody()->GetPosition().x > DEFAULT_WIN_W * 0.5f ||
			players[i]->GetBody()->GetPosition().x < 0 ||
			players[i]->IsStomped())
		{
			players[i]->Respawn();
		}
	}
}